package cn.david.org.controller;


import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.PageList;
import cn.david.org.domain.DictionaryType;
import cn.david.org.query.DictionaryTypeQuery;
import cn.david.org.service.IDictionaryTypeDetailService;
import cn.david.org.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    @Autowired
    private IDictionaryTypeDetailService iDictionaryTypeDetailService;
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId()==null){
            dictionaryTypeService.add(dictionaryType);
            }else {
                dictionaryTypeService.upadte(dictionaryType);
            }
            return AjaxResult.me();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("添加失败"+e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id ){
        try {
            iDictionaryTypeDetailService.delByTypesId(id);
            dictionaryTypeService.del(id);


            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }


    }
    @GetMapping("/{id}")
    public DictionaryType queryById(@PathVariable("id") Long id){
        return dictionaryTypeService.getById(id);

    }
    @GetMapping
    public List<DictionaryType> getAll(){
        return dictionaryTypeService.getAll();
    }
    @PostMapping
    public PageList<DictionaryType> list(@RequestBody DictionaryTypeQuery query){

        return dictionaryTypeService.queryData(query);
    }
    @PatchMapping
    public AjaxResult deleteBys(@RequestBody Long[] ids){
        try {
            dictionaryTypeService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }

    }


}
